<template>
    <div id="app-box">
        <img class="bg-img" src="static/image/bg.png">
        <img v-if="isHome" @click="clickHandler" class="main" :src="shareSrc">
        <img v-if="isReceive" @click="runGame" class="main small-model" src="static/image/share-quan25.png">
        <img v-if="isRunGame" @click="toGame" src="static/image/share-iwant-game.png" class="main small-model">
    
    </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      shareSrc: "",
      query: {},
      isHome: true,
      isReceive: false,
      isRunGame: false
    };
  },
  created() {
    //打开领取模态框
    this.init();
    this.initParams();
  },
  methods: {
    init() {
      //生成30
      this.shareSrc = `static/image/share-friend-xuming${parseInt(Math.random() * 29 + 1)}.png`;
    },
    clickHandler() {
      this.isHome = false;
      this.isReceive = true;
    },
    async runGame() {
      await this.addCard(2);
      ///领取20券
      this.isReceive = false;
      this.isRunGame = true;
    },
    async toGame() {
      window.location.href = "begin.html";
    },
    async addCard(prize_id) {
      const CARD_ID = "p8-68jsG52kM_xMswUoY7GZAUR0I"; //30元 罗非鱼抵用券
      const GET_CARD = `http://h5.macocn.com/tilapia/jssdk/wx_card_ext?card_id=${CARD_ID}`;
      const LIST_CARD = "http://h5.macocn.com/tilapia/api/myPrize";
      const ADD_CARD = "http://h5.macocn.com/tilapia/api/receiveCallback";

      //获取卡券接口
      let { data } = await axios.get(GET_CARD);

      let cardExt = {
        timestamp: data.card_ext.timestamp,
        signature: data.card_ext.signature
      };

      wx.addCard({
        cardList: [
          {
            cardId: data.card_id,
            cardExt: JSON.stringify(cardExt)
          }
        ], // 需要添加的卡券列表
        async success(res) {
          let { data } = await axios.get(LIST_CARD);
          let { code, data: row = [], msg } = data;

          if (code == 1 && row.length) {
            //25元 prize_id =2  is_get 0 未领取  1已领取
            for (let i = 0; i < row.length; i++) {
              let item = row[i];
              //目标
              if (item.prize_id == prize_id) {
                //==0
                if (item.is_get == 0) {
                  let { data: pdata = {} } = await axios({
                    url: ADD_CARD,
                    params: {
                      id: item.id
                    }
                  });
                }
              }
            }
            Toast("领取成功");
          } else {
            Toast(msg);
          }
        }
      });
    },
    initParams() {
      this.query = this.URLObject();
    },
    URLObject(url) {
      url = url || window.location.search;
      var theRequest = {};
      var strs = "";
      if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = decodeURIComponent(
            strs[i].split("=")[1]
          );
        }
      }
      return theRequest;
    }
  }
};
</script>

<style>

</style>
